<?php
/**
 * @version $Id: Reference.php 174 2008-02-24 15:56:02Z mikhail.miguel $
 */

require_once dirname(dirname(dirname(dirname(__FILE__)))).'/lib/trackback/spamcheck.php';
require_once(XOOPS_ROOT_PATH.'/class/snoopy.php');

class SpamCheck_Reference extends SpamCheck {
    var $_options = array(
        'sources'       => array(),
        'elements'      => array(
            'url'
        )
    );

    function SpamCheck_Reference($options=null)
    {
        if (is_array($options)) {
            foreach ($options as $key => $val) {
                $this->_options[$key] = $val;
            }
        }
    }

    function check($trackback)
    {
        $result = $this->_checkSource($this->_options['sources'], $trackback);
        if(TrackbackService::isError($result)) {
            $this->_results = true;
            return $result;
        }
        return false;
    }

    function _checkSource(&$sources, $trackback)
    {
        foreach($this->_options['elements'] as $element) {
            if(TrackbackService::isError($url = $trackback->get($element))) {
                return TrackbackService::setError($url->getMessage().'<br />'.' Cannot procede without it.');
            }
            if(empty($url)) {
                return TrackbackService::setError('Missing url. Cannot procede without it.');
            }
            if(!d3blog_validateUrl($url)) {
                return TrackbackService::setError('Invalid url. Cannot procede without it.');
            }

            // check the remote site
            $snoopy = new Snoopy;
            if ($snoopy->fetch($url)) {
                $sender_contents = $snoopy->results;
            } else {
                return TrackbackService::setError('Could not reach your blog url: '.$snoopy-error);
            }

            foreach($sources as $source) {
                if(false === strpos($sender_contents, $source))
                    continue;
                else
                    return true;
            }
        }
        return TrackbackService::setError('Missing reference to me in your blog. Cannot accept your trackback.');
        unset($snoopy);
    }
}
?>